#include "log.hpp"

#include <QDateTime>
#include <QWidget>
#include <QApplication>

void Log::Log::setFileName(const QString & fileName)
{
    _file.setFileName(fileName);
    _file.open(QIODevice::WriteOnly | QIODevice::Truncate); // Nettoie les précédents logs

    QTextStream out(&_file);

    out << "Created on " << QDateTime::currentDateTime().toString("yyyy-MM-dd-hh:mm:ss") << endl << endl;
}

void Log::Log::write(const char * msg, const char * msg2)
{
    QTextStream out(&_file);

    out << msg;

    if (msg2 != 0)
        out << msg2 << endl;
    else
        out << endl;
}

void Log::init(const QString & fileName)
{
    Log::Log * file = Log::Log::getInstance();
    file->setFileName(fileName);
}

void Log::myMessageOutput(QtMsgType type, const char * msg)
{
    Log::Log * file = Log::Log::getInstance();

    switch (type)
    {
    case QtDebugMsg:
        fprintf(stderr, "Debug : %s\n", msg);
        file->write("Debug : ", msg);
        break;
    case QtWarningMsg:
        fprintf(stderr, "Network : %s\n", msg);
        file->write("Network : ", msg);
        break;
    case QtCriticalMsg:
        fprintf(stderr, "Critical : %s\n", msg);
        file->write("Critical : ", msg);
        break;
    case QtFatalMsg:
        fprintf(stderr, "Fatal : %s\n", msg);
        file->write("Fatal : ", msg);
        qApp->quit();
    }
}
